﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Drawing.Printing;

namespace EZBarTender_Wrapper.EDLP
{
	public partial class TearListPrintPreview : Form
	{
		private PrinterSettings _settings;
		private string _connectionString;

		public string ConnectionString { get { return _connectionString; } }

		public TearListPrintPreview()
		{
			InitializeComponent();
		}

		public TearListPrintPreview(string connectionString)
		{
			InitializeComponent();
			_connectionString = connectionString;
		}

		public TearListPrintPreview(PrinterSettings settings, string connectionString)
		{
			InitializeComponent();
			_settings = settings;
			_connectionString = connectionString;
		}

		private void TearListPrintPreview_Load(object sender, EventArgs e)
		{
			DataTable table = new DataTable();
			using (SqlConnection connection = new SqlConnection(_connectionString))
			{
				connection.Open();
				using (SqlCommand command = new SqlCommand("execute sp_TearList", connection))
				{
					using (SqlDataAdapter adapter = new SqlDataAdapter(command))
					{
						adapter.Fill(table);
					}
				}
			}
			ui_dataGridView.DataSource = table;
		}

		private void ui_printButton_Click(object sender, EventArgs e)
		{

			DialogResult printDialogResult = printDialog.ShowDialog();

			if (printDialogResult == System.Windows.Forms.DialogResult.OK)
			{
				_settings = printDialog.PrinterSettings;

				if (_settings != null)
				{
					DocumentPrinter printer = new DocumentPrinter(_settings, ui_dataGridView);
					printer.PrinterSettings = _settings;
					printer.Print();

					EdlpInfo.ClearTearList(ConnectionString);
					/*
					DialogResult result = MessageBox.Show(
						this,
						"Was printing successful?",
						"Success?",
						MessageBoxButtons.YesNo,
						MessageBoxIcon.Exclamation,
						MessageBoxDefaultButton.Button1);

					if (result == DialogResult.Yes)
					{
						
					}
					 */
				}
			}
		}
	}
}